<?php
/*******************************************************************************
* 
* Poll4All::functions.php
* 
* Description: Useful functions for Poll4All
* Author: C4F Team (http://code4fun.org)
*  
*******************************************************************************/

/**
 * Print the poll 
 * 
 * @param String $path	-	Data file path
 * @param String $img	-	Images' folder path
 */
function poll($path="txt/txtdb.ini.php", $img="images/") {

	// Create and configure new ConfigMagik-Object
    $dbTXT = new ConfigMagik( $path, true, true);
    $dbTXT->SYNCHRONIZE = false;

    // if poll exist...
    if($dbTXT->get('pollid','MAIN')!=null) {

		// If a vote is cast, show poll result...
		if ( (isset($_POST['choice'])) && ($_POST['choice']!="") ) pollPrint($dbTXT, $img);
		// ... else show poll form
		else pollForm($dbTXT);
    }
    // if poll doesn't exist...
    else print "<div style=\"text-align: center;\">- No Poll Available -</div>";
}


/**
 * Print poll results
 * 
 * @param ConfigMagik $dbTXT	-	ConfigMagik object used to manage data file
 * @param String $img			-	Images' folder path
 */
function pollPrint(&$dbTXT, $imgPath="images/") {

	global $border,$width,$bgColor,$percentageBg,
           $cellpadding,$questionAlign,$optionsAlign,
           $questionColor,$optionColor,$questionBgColor,
           $font,$answerSize,$RandomColors,$questionSize,$barBg;

	// Init the bar chart colors array
    $colors = array(1,2,3,4,5,6,7);
	if ($RandomColors) shuffle($colors);
                
	$numAnswers = $dbTXT->get('numAnswers', 'MAIN');
	$question = $dbTXT->get('question', 'MAIN');
		
	$totVotes = getTotalVotes($dbTXT);
		
	$width=$width-($cellpadding*2);

	echo "<!-- Poll Box -->
	      <div style=\"width: ".$width."px; background-color: ".$bgColor."; border: ".$border."px solid #999999; padding: ".$cellpadding."px ".$cellpadding."px ".$cellpadding."px ".$cellpadding."px;\">
	      <!-- QUESTION -->
	      <div style=\"text-align: ".$questionAlign.";  background-color: ".$questionBgColor."; font-family: ".$font.",Arial, sans-serif; font-size: ".$questionSize."px; color: ".$questionColor."; padding: 5px;\">$question</div>
	      <!-- Separator -->
	      <div style=\"width: ".$width."px; height: 5px;\">&nbsp;</div>";
                      
	// Print Results
    for ($i=0; $i<$numAnswers; $i++) {
                
		$answerKey = "an".($i+1);
		$pollKey = $answerKey."poll";
		$answer = $dbTXT->get($answerKey,"ANSWERS");
		$poll = $dbTXT->get($pollKey,"ANSWERS");

		// Get percentage
		if ($poll!=0) {$percentage=(($poll*100)/$totVotes);}
		else $percentage=0;
                        
		if ($percentage) {
			
			$w = round((($width*0.01)*$percentage));
			$barChart = "<img src=\"".$imgPath."/".$colors[$i].".gif\" width=\"$w\" height=\"8\" alt=\"Bar Chart\" />";
		}
		else $barChart = "<img src=\"".$imgPath."0.gif\" width=\"8\" height=\"8\" alt=\"no result\" />";

        echo "<!-- Text Answer -->
		      <div style=\"text-align:".$optionsAlign."; background-color: ".$percentageBg."; font-family: ".$font.",Arial; font-size: ".$answerSize."px; color: ".$optionColor.";\">$answer: $poll votes (".round($percentage,1)."%)</div>
		      <!-- Bar Chart-->
		      <div style=\"text-align: left; background-color: ".$barBg.";\">$barChart</div>
		      <!-- Separator -->
		      <div style=\"width: ".$width."px; height: 5px;\">&nbsp;</div>";
	}
	// Print total votes
	echo "<div style=\"text-align: center; font-size: ".$answerSize."px;\">total votes: $totVotes</div></div>";
}


/**
 * Print voting form
 * 
 * @param ConfigMagik $dbTXT	-	ConfigMagik object used to manage data file
 */
function pollForm(&$dbTXT) {

	global $border,$width,$cellpadding,$questionAlign,$questionBgColor,
	       $font,$answerSize,$optionColor,$optionsAlign,$bgColor,$oddBgColor,
	       $evenBgColor,$questionSize,$questionColor;

    $currentFile= $_SERVER["SCRIPT_NAME"];
    if ( isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']!="" ) $query="?".$_SERVER['QUERY_STRING'];

    $numAnswers = $dbTXT->get('numAnswers', 'MAIN');
	$question = $dbTXT->get('question', 'MAIN');
	$totVotes = getTotalVotes($dbTXT);
	
    if ( isset($_COOKIE['poll4all']) && $_COOKIE['poll4all']==0 ) print "<div style=\"text-align: center;\">- Cookie reset successful -</div>";

    echo "<!-- Poll Form Table -->
	      <form name=\"poll\" method=\"post\" action=\"".$currentFile.$query."\">
          <table style=\"border: ".$border."px solid #000000; width: ".$width."px; padding: ".$cellpadding."px ".$cellpadding."px ".$cellpadding."px ".$cellpadding."px; background-color: ".$bgColor.";\">
	      <!-- QUESTION -->
	      <tr><td colspan=\"2\" style=\"text-align: ".$questionAlign."; background-color: ".$questionBgColor."; font-family: ".$font.",Arial, sans-serif; font-size: ".$questionSize."px; color: ".$questionColor.";\">$question</td></tr>";

    for ($i=1; $i<=$numAnswers; $i++) {
		
		$anKey = "an".$i;
		$answer = $dbTXT->get($anKey, 'ANSWERS');
		if ( $i&1 ) $bgColor = $oddBgColor;
        else        $bgColor = $evenBgColor;

        echo  "<!-- Answers -->
	    	   <tr><td style=\"width: ".($width-10)."px; text-align: ".$optionsAlign."; border-style: solid; border-width: 0px; margin: 0px; font-size: ".$answerSize."px; background-color: ".$bgColor.";\">$answer</td>
	       	   <td style=\"width: 10px; border-style: solid; border-width: 0px; margin: 0px; background-color: ".$bgColor.";\"><input type=\"radio\" name=\"choice\" value=\"$i\" /></td>
	       	   </tr>";
	}
              
    echo "<!-- Submit Button -->
	      <tr><td colspan=\"2\" style=\"text-align: center; width: ".$width."px; padding: 5px 5px 0px 5px;\">
	      <input type=\"submit\" name=\"submit\" value=\">> cast my vote <<\" /></td>
	      </tr></table></form>";
}


/**
 * Get total votes
 * 
 * @param ConfigMagik $dbTXT	-	ConfigMagik object used to manage data file
 * @return						-	Total votes 
 */
function getTotalVotes(&$dbFile) {

	$tot=0;
	$numAnswers = $dbFile->get('numAnswers', 'MAIN');

	for ($i=1; $i<=$numAnswers; $i++) {
		$pollKey = "an".$i."poll";
		$tot+=$dbFile->get($pollKey,"ANSWERS");
	}

	return $tot;
}

?>